<template>
  <div class='container'>
    <el-card>
      <!-- 筛选 -->
      <el-row>
        <el-col :span="21">
          <el-form label-width="70px">
            <el-form-item label="关键字">
              <el-input
                v-model="reqParams.keyword"
                placeholder="根据编号搜索"
                style="width:300px"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col
          :span="3"
          label-position="right"
        >
          <el-button
            size="mini"
            @click="clear()"
          >
            清除
          </el-button>
          <el-button
            size="mini"
            type="primary"
            @click="search()"
          >
            搜索
          </el-button>
        </el-col>
      </el-row>
      <!-- 警示栏 -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom:15px"
      >
      </el-alert>
      <!-- 表格 -->
      <template>
        <el-table :data="tableData">
          <el-table-column
            type="index"
            label="编号"
            width="120"
          >
          </el-table-column>
          <el-table-column
            label="题型"
            width="120"
          >
            <template
              slot-scope="scope">{{scope.row.questionType==="1"?'单选':(scope.row.questionType==="2"?'多选':'简答')}}
            </template>
          </el-table-column>
          <el-table-column
            prop="id"
            label="题目编号"
          ><template slot-scope="scope">
              <el-link
                @click="preview(scope.row.id)"
                type="primary"
                :underline="false"
              >
                <p>{{scope.row.id}}</p>
              </el-link>

            </template>
          </el-table-column>
          <el-table-column
            prop="addTime"
            label="录入时间"
          >
          </el-table-column>
          <el-table-column
            prop="totalSeconds"
            label="答题时间(s)"
            width="150"
          >
          </el-table-column>
          <el-table-column
            prop="accuracyRate"
            label="正确率(%)"
          >
          </el-table-column>
          <el-table-column
            prop="userName"
            label="录入人"
          >
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                @click="removeTableDate(scope.row.id)"
                type="danger"
                class="del"
                icon="el-icon-delete"
                circle
                plain
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <el-pagination
        class="pager"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="reqParams.pagesize"
        :current-page="reqParams.page"
        @current-change="changePager"
        @size-change="changeSize"
        :page-sizes="[20,30,40,50]"
      >
      </el-pagination>
      <!-- 弹出框 -->
    </el-card>
    <questions-preview
      :showPreviewDialog.sync="showPreviewDialog"
      :preSubjectId="preSubjectId"
      ref="questionsPreview"
    ></questions-preview>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
import questionsPreview from '../components/questions-preview.vue'
export default {
  data () {
    return {
      reqParams: {
        // 关键字
        keyword: null,
        // page
        page: 1,
        // 页尺寸
        pagesize: 10
      },
      // 组题列表
      tableData: [],
      // 总条数
      total: null,
      preSubjectId: '',
      showPreviewDialog: false

    }
  },
  components: {
    questionsPreview
  },
  created () {
    this.getTabledata()
  },
  methods: {
    async getTabledata () {
      const { data } = await randoms(this.reqParams)
      console.log(data)
      this.tableData = data.items
      this.total = data.counts
    },
    // 切换分页
    changePager (newPage) {
      this.reqParams.page = newPage
      this.getTabledata()
    },
    // 切换/尺码
    changeSize (newSize) {
      this.reqParams.page = 1
      this.reqParams.pagesize = newSize
      this.getTabledata()
    },
    // 搜索
    search () {
      this.reqParams.page = 1
      this.getTabledata()
    },

    // 清楚
    clear () {
      this.reqParams = {
        // 关键字
        keyword: null,
        // page
        page: 1,
        // 页尺寸
        pagesize: 10
      }
    },

    // 删除操作
    removeTableDate (id) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeRandoms({ id })
        this.$message.success('删除成功')
        this.getTabledata()
      }).catch(() => { })
    },

    // 弹层
    async preview (id) {
      try {
        this.showPreviewDialog = true
        this.preSubjectId = id
        await this.$refs.questionsPreview.getQuestionInfo(id)
        this.showPreviewDialog = true
      } catch (error) {
        this.$message.info('数据未找到')
        return error
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 10px 15px;
  font-size: 10px;
  .pager {
    margin: 15px 0;
    float: right;
  }
}
</style>
